package controllers;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Page;
import models.CustomerInformation;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.CustomerInformation.customerlist;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User: Thanhnv
 * Date: 4/22/12
 * Time: 9:27 AM
 * To change this template use File | Settings | File Templates.
 */
public class CustomerController  extends Controller {
    public static Result GO_HOME = redirect(
            routes.CustomerController.list(0, "name", "asc", "")
    );
   public static Result list(int page, String sortBy, String order, String filter) {
        Page<CustomerInformation> one = CustomerInformation.page(page, 10, sortBy, order, filter);
        System.out.println("aaaa" + one.getTotalRowCount());
        return ok(
                customerlist.render(one,sortBy, order, filter)
        );
    }

    public static Result initData(){
        List<CustomerInformation> customers = new ArrayList<CustomerInformation>();
        for(int i = 0; i< 23;i++){
            CustomerInformation customer = new CustomerInformation();
            customer.name = "Customer" + i;
            customer.address = "Address of customer" + i;

            customers.add(customer);
        }
        Ebean.beginTransaction();
        Ebean.save(customers);
        Ebean.commitTransaction();
        return ok("ok");
    }
    public static Result create() {
        Form<CustomerInformation> customerInformationForm = form(CustomerInformation.class);
        return ok(views.html.CustomerInformation.createCustomerForm.render(customerInformationForm)
        );
    }

    public static Result save() {
        Form<CustomerInformation> customerInformationForm = form(CustomerInformation.class).bindFromRequest();
        if(customerInformationForm.hasErrors()) {
            return badRequest(views.html.CustomerInformation.createCustomerForm.render(customerInformationForm));
        }
        customerInformationForm.get().save();
        flash("success", "Customer " + customerInformationForm.get().name + " has been created");
        return GO_HOME;
    }

    public static Result edit(Long id) {
        Form<CustomerInformation> computerForm = form(CustomerInformation.class).fill(
                CustomerInformation.find.byId(id)
        );
        return ok(
                views.html.CustomerInformation.editCustomerForm.render(id, computerForm)
        );
    }

    public static Result update(Long id) {
        Form<CustomerInformation> customerInformationForm = form(CustomerInformation.class).bindFromRequest();
        if(customerInformationForm.hasErrors()) {
            return badRequest(views.html.CustomerInformation.editCustomerForm.render(id, customerInformationForm));
        }
        customerInformationForm.get().update(id);
        flash("success", "Thông tin khách hàng " + customerInformationForm.get().name + " đã được cập nhật");
        return GO_HOME;
    }

    public static Result delete(Long id) {
        CustomerInformation.find.ref(id).delete();
        flash("success", "Khách hàng đã bị xóa");
        return GO_HOME;
    }
}